//
//  ApplyOpenInvoicListVC.m
//  NtoccShipper
//
//  Created by mac on 2020/1/3.
//  Copyright © 2020 zhuchongjin. All rights reserved.
//

#import "ApplyOpenInvoicListVC.h"
#import "ApplyOpenInvoiclistCell.h"
#import "ApplyOpenInvoiclistModel.h"
#import "AddNewInvoiceInfoVC.h"
#import "ApplyOpenInvoicVC.h"

@interface ApplyOpenInvoicListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) ApplyOpenInvoiclistCell *applyOpenInvoiclistCell;
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;
@property (weak, nonatomic) IBOutlet UIButton *btnSave;
@end

@implementation ApplyOpenInvoicListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:@"发票索取"];
    [self setBackLeftButton:@""];
    self.btnSave.backgroundColor = [UIColor cjMainColor];
    self.applyOpenInvoiclistCell = [[ApplyOpenInvoiclistCell alloc]initWithFlex:nil reuseIdentifier:nil];
    [self.view addSubview:self.tableView];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
    [self initData];
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self.tableView.mj_header beginRefreshing];
}

- (void)initData{
    self.modelArray = [[NSMutableArray alloc]init];
    self.page = 1;
}

- (void)loadData{
    
    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/invoiceMessage",CJBaseUrl);
    NSDictionary *dicPar = @{
                @"pageNum":CJStringWithFormat(@"%d",self.page),
                @"pageSize":@"10",
                @"type":self.strOpenType,
                @"isInvoiceDefault":@"1"
    };
    
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strUrl parameters:dicPar success:^(id  _Nonnull responseObject) {

        DLog(@"++++++++%@",responseObject);
        NSArray *list = responseObject[@"records"];
        NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);
        if (list.count > 0) {

           if ([ws.refreshType isEqualToString:@"down"]) {
               [ws.modelArray removeAllObjects];
           }

           NSError * error ;
           NSArray *arr = [ApplyOpenInvoiclistModel arrayOfModelsFromDictionaries:list error:&error];
           [ws.modelArray addObjectsFromArray:arr];

           [ws.tableView.mj_header endRefreshing];
           [ws.tableView.mj_footer endRefreshing];

           if (ws.modelArray.count == total.intValue) {
               [ws.tableView.mj_footer endRefreshingWithNoMoreData];
           }

        }else{
           [ws.modelArray removeAllObjects];
           [ws.tableView.mj_header endRefreshing];
           [ws.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [ws.tableView reloadData];
       
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        [ws.tableView.mj_header endRefreshing];
        [ws.tableView.mj_footer endRefreshing];
    }];
}

- (void)downRefresh{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"ApplyOpenInvoiclistCell";
    ApplyOpenInvoiclistCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[ApplyOpenInvoiclistCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    ApplyOpenInvoiclistModel *model = self.modelArray[indexPath.row];
    [cell setCellInfoWithModel:model];
    WS(ws);
    cell.blockChange = ^(id anyObject) {
        [ws btnChangeInvioceActionWithModel:model];
    };
    cell.blockApply = ^(id anyObject) {
        [ws btnApplyInvioceActionWithModel:model];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ApplyOpenInvoiclistModel *model = self.modelArray[indexPath.row];
    ApplyOpenInvoicVC *vc = [ApplyOpenInvoicVC new];
    vc.model = model;
    [self pushVc:vc];
}

- (IBAction)btnAddOpenInvioceAction:(UIButton *)sender {
    WS(ws);
    AddNewInvoiceInfoVC *vc = [[AddNewInvoiceInfoVC alloc]init];
//    vc.strCode = model.code;
    vc.isChange = NO;
    vc.blockSuccess = ^(id anyObject) {
        [ws loadData];
    };
    [self pushVc:vc];
}

- (void)btnApplyInvioceActionWithModel:(ApplyOpenInvoiclistModel *)model{
    
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/invoice",CJBaseUrl);
    NSDictionary *dicPar = @{@"detailCodes":self.strCode,
                             @"invoiceMessageCode":model.code
    };
    
    WS(ws);
    [[HDNetworking sharedHDNetworking] jsonTaskWithHTTPMethod:PostType urlString:strURL withParameter:dicPar status:OperationTip success:^(id  _Nonnull responseObject) {
        if (ws.blockSucess) {
            ws.blockSucess(@"");
        }
        [ws pop];
    } failure:^(NSError * _Nonnull error) {
        
    }];
    
}

- (void)btnChangeInvioceActionWithModel:(ApplyOpenInvoiclistModel *)model{
    WS(ws);
    AddNewInvoiceInfoVC *vc = [[AddNewInvoiceInfoVC alloc]init];
    vc.strCode = model.code;
    vc.isChange = YES;
    vc.blockSuccess = ^(id anyObject) {
        [ws loadData];
    };
    [self pushVc:vc];
}

- (CJTableView *)tableView{
    
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, kTopHeight + CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - CJTableToTopHeight - self.btnSave.height),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(YES)
        ;
        WS(ws);
        _tableView.blockMjHeader = ^(BOOL isheader) {
            if (isheader) {
                [ws downRefresh];
            } else {
                [ws upRefresh];
            }
        };
    }
    return _tableView;
}


@end
